﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data;

namespace PaperManager
{
    public class MyContainer
    {
        public struct Itm
        { 
            public string Title, Author, KeyWords, Abstract;
        }
        public virtual bool add(Itm v)
        {
            return true;
        }
        public virtual Itm get(int idx)
        {
            Itm v;
            v.Title = "";
            v.Author = "";
            v.KeyWords = "";
            v.Abstract = "";
            return v;
        }
        public virtual bool set(int idx, Itm v)
        {
            bool rt = true;
            return rt;
        }
        public virtual int getCount()
        {
            int rt = 0;
            return rt;
        }
        public virtual bool remove(int idx)
        {
            bool rt = true;
            return rt;
        }
        public virtual ArrayList query(string queryS)
        {
            ArrayList rt = new ArrayList();
            return rt;
        }
        public bool CompString(string targetS, string queryS)
        {
            bool rt = false;
            int pos = 0;
            int pLim = targetS.Length - queryS.Length;
            int i;
            bool bMatch = false;
            while (pos <= pLim)
            {
                if(targetS[pos]==queryS[0])
                {
                    bMatch = true;
                    for (i = 1; i < queryS.Length; i++)
                    {
                        if (targetS[pos + i] != queryS[i])
                        {
                            bMatch = false;
                            break;
                        }
                    }
                    if (bMatch)
                    {
                        rt = true;
                        break;
                    }
                }
                pos++;
            }
            return rt;
        }
    }
    public class MyALContainer : MyContainer
    {
        private ArrayList _data;
        public MyALContainer()
        {
            _data=new ArrayList();
        }
        public override bool add(Itm v)
        {
            bool rt = true;
            _data.Add(v);
            return rt;
        }
        public override Itm get(int idx)
        {
            Itm v;
            v = (Itm)_data[idx];
            return v;
        }
        public override bool set(int idx, Itm v)
        {
            bool rt = true;
            _data[idx] = v;
            return rt;
        }
        public override int getCount()
        {
            return _data.Count;
        }
        public override bool remove(int idx)
        {
            bool rt = true;
            _data.RemoveAt(idx);
            return rt;
        }
        public override ArrayList query(string queryS)
        {
            ArrayList rt = new ArrayList();
            Itm tmp;
            for (int i = 0; i < _data.Count; i++)
            {
                tmp = (Itm)_data[i];
                if (CompString(tmp.Title, queryS) || CompString(tmp.Author, queryS) || CompString(tmp.KeyWords, queryS) || CompString(tmp.Abstract, queryS))
                {
                    rt.Add(tmp);
                }
            }
            return rt;
        }
    }
    public class MyDBContainer : MyContainer
    {
        private OleDbConnection _cnn;
        public MyDBContainer(string dbFileName)
        {
            //connect the database
            string cnnstr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbFileName + ";User Id=admin;Password=;";
            _cnn = new OleDbConnection(cnnstr);
            _cnn.Open();
        }
        ~MyDBContainer()
        {
            //disconnect the database
            //_cnn.close();
        }
        public override bool add(MyContainer.Itm v)
        {
            bool rt = true;
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText="insert into PapersInfo(Title,Author,Keywords,Abstract) values('"+v.Title+"','"+v.Author+"''"+v.KeyWords+"','"+v.Abstract+"')";
            cmd.ExecuteNonQuery();
            return rt;
        }
        public override MyContainer.Itm get(int idx)
        {
            int cnt = 0;
            Itm rt;
            rt.Title = "";
            rt.Author = "";
            rt.KeyWords = "";
            rt.Abstract = "";
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText = "select * from PapersInfo";
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (cnt == idx)
                {
                    rt.Title = reader.GetString(1);
                    rt.Author = reader.GetString(2);
                    rt.KeyWords = reader.GetString(3);
                    rt.Abstract = reader.GetString(4);
                    break;
                }
                cnt++;
            }
            return rt;
        }
        public override bool set(int idx, MyContainer.Itm v)
        {
            return base.set(idx, v);
        }
        public override int getCount()
        {
            int cnt = 0;
            OleDbCommand cmd = _cnn.CreateCommand();
            cmd.CommandText = "select * from PapersInfo";
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                cnt++;
            }
            return cnt;
        }
    }
}
